/**
 * 
 */
package com.swim.employeeswim.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.UserService;
import com.swim.employeeswim.entity.EmployeeSwim;
import com.swim.natatoriuminfotouser.entity.NatatoriumInfoToUser;
import com.swim.natatoriuminfotouser.service.NatatoriumInfoToUserService;
import com.swim.sys.entity.SysUserRole;
import com.swim.sys.service.SysUserRoleService;
import com.swim.employeeswim.dao.EmployeeSwimDao;

/**
 * 员工Service
 * @author 刘军武
 * @version 2021-04-23
 */
@Service
@Transactional(readOnly=true)
public class EmployeeSwimService extends CrudService<EmployeeSwimDao, EmployeeSwim> {

	@Autowired
	private NatatoriumInfoToUserService natatoriumInfoToUserService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private SysUserRoleService sysUserRoleService;
	
	/**
	 * 获取单条数据
	 * @param employeeSwim
	 * @return
	 */
	@Override
	public EmployeeSwim get(EmployeeSwim employeeSwim) {
		return super.get(employeeSwim);
	}
	
	/**
	 * 查询分页数据
	 * @param page 分页对象
	 * @param employeeSwim
	 * @return
	 */
	@Override
	public Page<EmployeeSwim> findPage(Page<EmployeeSwim> page, EmployeeSwim employeeSwim) {
		return super.findPage(page, employeeSwim);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param employeeSwim
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(EmployeeSwim employeeSwim) {
		//新增用户游泳馆信息关联数据
		NatatoriumInfoToUser natatoriumInfoToUser = new NatatoriumInfoToUser();
		natatoriumInfoToUser.setUserCode(employeeSwim.getUserCode());		// 用户user_code    
		natatoriumInfoToUser.setNatatoriumInfoId(employeeSwim.getNatatoriumInfoId());		// 游泳馆信息id
		natatoriumInfoToUserService.save(natatoriumInfoToUser);
		//把登录账号的角色换成选择的角色
		SysUserRole sysUserRole = new SysUserRole();
		sysUserRole.setUserCode(employeeSwim.getUserCode());
		List<SysUserRole> sysUserRoleList = sysUserRoleService.findList(sysUserRole);
		SysUserRole sysUserRoleOne = sysUserRoleList.get(0);
		sysUserRoleOne.setRoleCode(employeeSwim.getRole());
		sysUserRoleService.updateUserRole(sysUserRoleOne);
		//新增员工数据
		super.save(employeeSwim);
	}
	
	/**
	 * 校验登录账号是否存在
	 * @param userCode		 登录账号
	 * @return Boolean true表示账号存在 ，false表示账号不存在
	 */
	public Boolean checkUserCodeIsExist(String userCode) {
		User user = new User();
		user.setLoginCode(userCode);
		User resultUser = userService.getByLoginCode(user);
		if (resultUser != null) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 更新状态
	 * @param employeeSwim
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(EmployeeSwim employeeSwim) {
		super.updateStatus(employeeSwim);
	}
	
	/**
	 * 删除数据
	 * @param employeeSwim
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(EmployeeSwim employeeSwim) {
		//删除NatatoriumInfoToUser记录
		NatatoriumInfoToUser natatoriumInfoToUser = new NatatoriumInfoToUser();
		natatoriumInfoToUser.setUserCode(employeeSwim.getUserCode());		// 用户user_code    
		natatoriumInfoToUser.setNatatoriumInfoId(employeeSwim.getNatatoriumInfoId());		// 游泳馆信息id
		List<NatatoriumInfoToUser> natatoriumInfoToUserList = natatoriumInfoToUserService.findList(natatoriumInfoToUser);
		natatoriumInfoToUserService.delete(natatoriumInfoToUserList.get(0));
		//把员工的角色换成选择的user
		SysUserRole sysUserRole = new SysUserRole();
		sysUserRole.setUserCode(employeeSwim.getUserCode());
		List<SysUserRole> sysUserRoleList = sysUserRoleService.findList(sysUserRole);
		SysUserRole sysUserRoleOne = sysUserRoleList.get(0);
		sysUserRoleOne.setRoleCode(employeeSwim.getRole());
		sysUserRoleOne.setRoleCode("user");
		sysUserRoleService.updateUserRole(sysUserRoleOne);
		//删除员工数据
		super.delete(employeeSwim);
	}
	
}